package cn.lwq.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lwq.help.ConfigRead;
import cn.lwq.help.MyConnection;

/**
 * @since 2017年7月17日
 * @author lwq 719812998@qq.com
 * @description
*/
public class MysqlConnection implements MyConnection {
	
	public static Connection mysqlConnection = null;
	public String mysqlUrl;
	public static Map<String,List<String>> MAP_COLUMN_TYPE = null;
	
	public MysqlConnection() throws Exception {
		if(mysqlConnection == null) {
			Class.forName("com.mysql.jdbc.Driver");
			ConfigRead config = new ConfigRead();
			mysqlUrl = config.getDBUrl(ConfigRead.MYSQL);
			Connection connection = DriverManager.getConnection(mysqlUrl, ConfigRead.DATABASE_USER, ConfigRead.DATABASE_PASSWORD);
			mysqlConnection = connection;
		}
		if(MAP_COLUMN_TYPE == null) {
			MAP_COLUMN_TYPE = new HashMap<String, List<String>>();
			//@非空
			List<String> notnulls = new ArrayList<String>();
			notnulls.add("varchar");
			notnulls.add("set");
			notnulls.add("enum");
			notnulls.add("char");
			notnulls.add("tinytext");
			notnulls.add("text");
			notnulls.add("mediumtext");
			notnulls.add("longtext");
			///
			notnulls.add("float");
			notnulls.add("decimal");
			notnulls.add("numeric");
			notnulls.add("double");
			notnulls.add("bigint");
			notnulls.add("integer");
			notnulls.add("int");
			notnulls.add("mediumint");
			notnulls.add("smallint");
			notnulls.add("tinyint");
			MAP_COLUMN_TYPE.put(ConfigRead.$NOT_NULL, notnulls);
			//@length
			List<String> lengths = new ArrayList<String>();
			lengths.add("varchar");
			lengths.add("set");
			lengths.add("enum");
			lengths.add("char");
			lengths.add("tinytext");
			lengths.add("text");
			lengths.add("mediumtext");
			lengths.add("longtext");
			MAP_COLUMN_TYPE.put(ConfigRead.$LENGTH, lengths);
			//@max
			List<String> maxs = new ArrayList<String>();
			maxs.add("float");
			maxs.add("decimal");
			maxs.add("numeric");
			maxs.add("double");
			maxs.add("bigint");
			maxs.add("integer");
			maxs.add("int");
			maxs.add("mediumint");
			maxs.add("smallint");
			maxs.add("tinyint");
			MAP_COLUMN_TYPE.put(ConfigRead.$MAX, maxs);
			//@日期型
			List<String> dates = new ArrayList<String>();
			dates.add("date");
			dates.add("year");
			MAP_COLUMN_TYPE.put(ConfigRead.$DATE, dates);
		}
	}
	
	@Override
	public Connection getConnection() {
		return mysqlConnection;
	}

}
